<?php
declare (strict_types=1);

namespace app\organization\controller\core;

use app\common\controller\Frontend;
use app\common\model\banxi\core\CoreCourseAppointment as CoreCourseAppointmentModel;
use app\common\model\banxi\organization\Organization as OrganizationModel;
use app\common\service\course\CourseAppointmentService;
use think\db\exception\DbException;

class CoreCourseAppointment extends Frontend
{
    protected array $noNeedLogin = [];
    protected array $noNeedPermission = [];

    public function initialize(): void
    {
        parent::initialize();
        $this->model = new CoreCourseAppointmentModel();
    }


    /**
     * 分页查询
     * @return void
     * @throws DbException
     */
    public function listPage(): void
    {
        $param = $this->request->param(['index', 'limit', 'orderBy', "query"]);
        $organizationId = $this->auth->organizationId;
        $organizationModel = new OrganizationModel();
        $organizationIds = $organizationModel->getSonOrganization($organizationId);
        $param['query']['IN_studentOrganizationId'] = $organizationIds;
        //查询条件解析
        $where = get_query($param);
        //获取分页数据
        $pageParams = get_page_params($param);
        $list = $this->model->getListPage($where, $pageParams['page'], $pageParams['limit']);
        $this->ok('操作成功', result_row($list));
    }

    /**
     * 查询
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function get()
    {
        $param = $this->request->param(["query" => []]);
        if (empty($param['query'])) {
            $this->fail('请传递你要查询的记录');
        }
        $where = get_query($param);
        $info = $this->model->getInfo($where);
        $this->ok('操作成功', $info);
    }

    public function trainerAppointment()
    {
        $param = $this->request->param();
        $organizationId = $this->auth->organizationId;
        $courseAppointmentService = new CourseAppointmentService();
        $res = $courseAppointmentService->trainerAppointment($param, $organizationId);
        if (!$res) {
            $this->fail($courseAppointmentService->getError());
        }
        $this->ok();
    }

    public function timeAppointment()
    {
        $param = $this->request->param();
        $organizationId = $this->auth->organizationId;
        $courseAppointmentService = new CourseAppointmentService();
        $res = $courseAppointmentService->timeAppointment($param, $organizationId);
        if (!$res) {
            $this->fail($courseAppointmentService->getError());
        }
        $this->ok();
    }

    public function updateAppointment()
    {
        $param = $this->request->param();
        $organizationId = $this->auth->organizationId;
        $courseAppointmentService = new CourseAppointmentService();
        $res = $courseAppointmentService->updateAppointment($param, $organizationId);
        if (!$res) {
            $this->fail($courseAppointmentService->getError());
        }
        $this->ok();
    }

    public function cancelAppointment()
    {
        $param = $this->request->param();
        $organizationId = $this->auth->organizationId;
        $courseAppointmentService = new CourseAppointmentService();
        $res = $courseAppointmentService->cancelAppointment($param, $organizationId);
        if (!$res) {
            $this->fail($courseAppointmentService->getError());
        }
        $this->ok();
    }
}
